﻿
function array_checkbox(array_object) {
    var array_return = new Array();
    $.each(array_object, function (i, obj) {
        var object = {};
        object.name = obj.name;
        object.value = obj.value;
        object.text = obj.text;
        object.checked = obj.checked;        
        array_return.push(object);
    });
    return array_return;
}

function array_select(array_object) {
    var array_return = new Array();
    $.each(array_object, function (i, obj) {
        var object = {};
        object.value = obj.value;
        object.text = obj.text;
        object.selected = obj.selected;
        array_return.push(object);
    });
    return array_return;
}


//CheckActionInPath
function CheckAtionInPath(action) {
    var flag = false;
    var path = window.location.pathname;
    var array = path.split('/');
    for (var i = 0; i < array.length; i++) {
        if (array[i] == action) {
            flag = true;
            break;
        }
    }
    return flag;
}

//Get ID from URL
function GetId() {
    var Id = "";
    var path = window.location.pathname;
    if (path.indexOf("/edit/") !== -1 || path.indexOf("/view/") !== -1) {
        var index = (path.indexOf("/edit/") >= 0) ? path.indexOf("/edit/") + 6 : path.indexOf("/view/") + 6;
        Id = path.substring(index);

    }
    return Id.trim();
}
//get all input checked
function InputChecked() {
    var array = new Array();
    $("input[name='chkItem']:checked").each(function (i) {
        array[i] = this.value;
    });
    return array;
}


////Scroll ToolBar
//$(document).ready(function () {
//    $(".aspNetHidden").remove();
//    $(".__VIEWSTATE").remove();
//    var top = $("#toolbar_backend").offset().top - parseFloat($("#toolbar_backend").css("marginTop").replace(/auto/, 0));
//    $(window).scroll(function (event) {
//        var y = $(this).scrollTop();
//        if (y >= (top - 5))
//            $("#toolbar_backend").css("position", "fixed").css("top", "50px").css("z-index", "1030");
//        else
//            $("#toolbar_backend").css("position", "static");
//    });
//});
//Modal Upload Show
$("#uploadfiles").click(function () {
    $("#uploadfiles").modal({
        show: false,
        remote: true
    });
});

//Modal Change Pass Show
$("#changepass").click(function () {
    $("#changepass").modal({
        show: false,
        remote: true
    });
});


//Check All item in table
$(":input[name='chkAll']").live("click", function () {
    if ($(this).prop("checked")) {
        $(':input[name$="chkItem"]').each(function () {
            $(this).prop("checked", true);
        });
    } else {
        $(':input[name$="chkItem"]').each(function (item) {
            $(this).prop("checked", false);
        });
    }
});

//Tham khao ham hay
//bacon("a", "b", "c");
function bacon() {
    for (var i in arguments) {
        if (arguments.hasOwnProperty(i)) {
            alert(arguments[i]);
        }
    }
}

//Get value element in form
jQuery.fn.serializeObject = function () {
    var self = this,
        json = {},
        push_counters = {},
        patterns = {
            "validate": /^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
            "key": /[a-zA-Z0-9_]+|(?=\[\])/g,
            "push": /^$/,
            "fixed": /^\d+$/,
            "named": /^[a-zA-Z0-9_]+$/
        };

    this.build = function (base, key, value) {
        base[key] = value;
        return base;
    };

    this.push_counter = function (key) {
        if (push_counters[key] === undefined) {
            push_counters[key] = 0;
        }
        return push_counters[key]++;
    };

    jQuery.each(jQuery(this).serializeArray(), function () {

        // skip invalid keys
        if (!patterns.validate.test(this.name)) {
            return;
        }

        var k,
            keys = this.name.match(patterns.key),
            merge = this.value,
            reverse_key = this.name;

        while ((k = keys.pop()) !== undefined) {

            // adjust reverse_key
            reverse_key = reverse_key.replace(new RegExp("\\[" + k + "\\]$"), '');

            // push
            if (k.match(patterns.push)) {
                merge = self.build([], self.push_counter(reverse_key), merge);
            }

            // fixed
            else if (k.match(patterns.fixed)) {
                merge = self.build([], k, merge);
            }

            // named
            else if (k.match(patterns.named)) {
                merge = self.build({}, k, merge);
            }
        }

        json = jQuery.extend(true, json, merge);
    });


    return JSON.stringify(json);
};
